package com.zrx.app.android.modules;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.zrx.app.android.R;
import com.zrx.app.android.UTIL.ZRXUTIL;
import com.zrx.app.android.ZRXApplication;

import java.io.Serializable;
import java.text.SimpleDateFormat;

/**
 * Created by liuji on 2017/5/20.
 */

public abstract class BaseModule implements Serializable{
    private static final String TAG = "BaseModule";
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public abstract String getMainURL();
    public abstract void startActivity(Context context,JSONObject record);

    public View getView(final JSONObject record,final ViewGroup vg) {
        LayoutInflater inflater = (LayoutInflater) ZRXApplication.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_list_element, vg,false);

        GradientDrawable drawable = new GradientDrawable();
        view.setBackground(drawable);
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setStroke(0, Color.BLACK);
        drawable.setCornerRadius(8);
        try {
            JSONObject basicData =null;
            if(record.containsKey("basicData"))
                basicData=record.getJSONObject("basicData");
          /*  Log.e("传入数据22",String.valueOf(basicData));*/
            String name="未知";
            String idCardNum="未知";
            String tel="未知";
            String state="未知";
            String createdDate="未知";
            String modifiedDate="未知";

            if(basicData!=null){
                if(basicData.containsKey("申请人"))
                    name=basicData.getString("申请人");
                else if(basicData.containsKey("融资人"))
                    name=basicData.getString("融资人");
                if(basicData.containsKey("身份证号")) {
                    idCardNum = basicData.getString("身份证号");
                    if(idCardNum.length()>11)
                        idCardNum=idCardNum.substring(0,6)+"********"+idCardNum.substring(14);
                }
                if(basicData.containsKey("手机号")) {
                    tel = basicData.getString("手机号");
                    if(tel.length()>4)
                        tel=tel.substring(0,tel.length()-4)+"****";
                }
            }

            String rawState = record.getString("state");
            drawable.setColor(ZRXUTIL.translateStateToColor(rawState));
            if(record.containsKey("state"))
                state = ZRXUTIL.translateState(rawState);
            if(record.containsKey("createdDate"))
                createdDate=sdf.format(record.getDate("createdDate"));
            if(record.containsKey("modifiedDate"))
                modifiedDate=sdf.format(record.getDate("modifiedDate"));

            TextView lbName = (TextView) view.findViewById(R.id.label_name);
            TextView lbIdCardNum = (TextView) view.findViewById(R.id.label_idCardNum);
            TextView lbTel = (TextView) view.findViewById(R.id.label_tel);
            TextView lbState = (TextView) view.findViewById(R.id.label_state);
            TextView lbCreatedDate = (TextView) view.findViewById(R.id.label_createdDate);
            TextView lbModifiedDate = (TextView) view.findViewById(R.id.label_modifiedDate);
            lbName.setText(name);
            lbIdCardNum.setText(idCardNum);
            lbTel.setText(tel);
            lbState.setText(state);
            lbCreatedDate.setText(createdDate);
            lbModifiedDate.setText(modifiedDate);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(vg.getContext(),record);
                }
            });
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        return view;
    }

    public abstract String getModuleName();
}
